﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

#region | Imports |

using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web.Services.Description;
using DataAccess;
using DataAccess.DBConnections;
using DataAccess.ProxyConnections;
using Microsoft.VisualBasic;
using Oracle.DataAccess.Client;
using Web.Core;

#endregion

namespace Web.Business
{
    public partial class ApplicationBal
    {
        #region | Conts |

        public static DataAccessClient dal;//= new DataAccessClient(DATABASE_PROXY.AUTH);

        private static DBConnectionsEngine dbEngine;

        private static OracleConnection Conn;

        private static Hashtable hashtable;

        private static ApplicationBal _dal;

        public static object result = null;
        
        private static System.String sql = "";

        private static String _err = null;

        public static ApplicationBal GetInstance()
        {
            /* for Init */
            dal = new DataAccessClient(DATABASE_PROXY.AUTH);

            dbEngine = new DBConnectionsEngine();

            Conn = new DatabaseProxy().GetAuthConnection();

            _dal = new ApplicationBal();

            return _dal;
        }

        #endregion

        #region | Helper |

        internal string GetApplAutoGen(System.Web.UI.Page page, 
            string companyCode = "HP00")
        {
            sql =
              string.Format(
                  @"SELECT MAX(CAST(LTRIM(APPLNO,'A') AS INT)) FROM Application WHERE COMPANY_CODE = '{0}' ",
                  companyCode);

            if (dal.ExecuteScalar(sql, null, out result, out _err) == DB_RESULT.COMPLETED)
            {
                return "A" + Convert.ToString(result);
            }
            else
            {
                Core.Modal.Exception(
                    page: page,
                    message: _err);
            }
            return null;
        }

        internal bool Insert(p_APPLICATION obj, out string err)
        {
            bool result = false;
            if (dal.Insert(obj, out err) != DB_RESULT.INCOMPLETED)
                result = true;

            return result;
        }

        internal bool Update(p_APPLICATION sender, out string err)
        {
            bool result = false;
            p_APPLICATION obj = new p_APPLICATION();
            obj.APPLNAME = sender.APPLNAME;
            obj.COMPANY_CODE = sender.COMPANY_CODE;
            obj.DESCRIPTION = sender.DESCRIPTION;
            obj.URL = sender.URL;
            obj.PDAFLAG = sender.PDAFLAG;
            obj.MOBILEURL = sender.MOBILEURL;
            obj.STATUS = sender.STATUS;
            obj.PRIVATE_FLAG = sender.PRIVATE_FLAG;
            obj.BUYER_FLAG = sender.BUYER_FLAG;
            obj.MC_FLAG = sender.MC_FLAG;
            obj.REMARK = sender.REMARK;
            obj.setCriteria(p_APPLICATION_FIELD.APPLNO, CRITERIA_FLAG.Equal, sender.APPLNO);
            obj.setCriteria(p_APPLICATION_FIELD.COMPANY_CODE, CRITERIA_FLAG.Equal, sender.COMPANY_CODE);
            obj.setOperator(OPERATOR.AND);
            if (dal.Update(obj, out err) == DB_RESULT.INCOMPLETED)
                result = true;

            return result;
        }

        internal bool Delete(p_APPLICATION sender, out string err)
        {
            bool result = false;
            p_APPLICATION obj = new p_APPLICATION();
            obj.setCriteria(p_APPLICATION_FIELD.COMPANY_CODE, CRITERIA_FLAG.Equal, sender.COMPANY_CODE);
            obj.setCriteria(p_APPLICATION_FIELD.APPLNO, CRITERIA_FLAG.Equal, sender.APPLNO);
            obj.setOperator(OPERATOR.AND);
            if (dal.Delete(obj, out err) == DB_RESULT.INCOMPLETED)
                result = true;

            return result;
        }

        #endregion

        #region | dbEngine |

        internal IEnumerable<T> GetApplAnonymousT<T>(
            System.Web.UI.Page page,
            T type,
            String companyCode)
        {
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (!String.IsNullOrEmpty(companyCode))
                {
                    concast.Append(" AND COMPANY_CODE=:COMPANY_CODE ");
                    hashtable.Add(":COMPANY_CODE", companyCode);
                }

                sql = System.String.Format(@" SELECT APPLNO ,APPLNAME FROM APPLICATION");

                sql = sql + concast.ToString();

                System.Data.DataTable dt = dbEngine.GetDataTable(
                                                               strSql: sql,
                                                               h: hashtable,
                                                               Conn: Conn);

                if (dt != null)
                    if (dt.Rows.Count > 0)
                    {
                        IQueryable query = (from o in dt.AsEnumerable()
                                            select new
                                            {
                                                applNo = o.Field<String>("APPLNO")
                                                ,
                                                applName = o.Field<String>("APPLNAME")
                                            }).ToList().AsQueryable();

                        return query as IEnumerable<T>;
                    }
            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                     page: page,
                     message: Core.Exception.ModalException(
                                         method: System.Reflection.MethodBase.GetCurrentMethod(),
                                         ex: ex));
            }
            return null;
        }

        internal IEnumerable<DataAccess.p_APPLICATION> Select(
            System.Web.UI.Page page, 
            DataAccess.p_APPLICATION obj)
        {
            IEnumerable<DataAccess.p_APPLICATION> entities = new List<DataAccess.p_APPLICATION>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (obj != null)
                {
                    if (!String.IsNullOrEmpty(obj.COMPANY_CODE))
                    {
                        concast.Append(" AND COMPANY_CODE=:COMPANY_CODE ");
                        hashtable.Add(":COMPANY_CODE", obj.COMPANY_CODE);
                    }

                    if (!String.IsNullOrEmpty(obj.PRIVATE_FLAG))
                    {
                        concast.Append(" AND PRIVATE_FLAG=:PRIVATE_FLAG ");
                        hashtable.Add(":PRIVATE_FLAG", obj.PRIVATE_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.MC_FLAG))
                    {
                        concast.Append(" AND MC_FLAG=:MC_FLAG ");
                        hashtable.Add(":MC_FLAG", obj.MC_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.BUYER_FLAG))
                    {
                        concast.Append(" AND BUYER_FLAG=:BUYER_FLAG ");
                        hashtable.Add(":BUYER_FLAG", obj.BUYER_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.NT_FLAG))
                    {
                        concast.Append(" AND NT_FLAG=:NT_FLAG ");
                        hashtable.Add(":NT_FLAG", obj.NT_FLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.REMARK))
                    {
                        concast.Append(" AND REMARK=:REMARK ");
                        hashtable.Add(":REMARK", obj.REMARK);
                    }

                    if (!String.IsNullOrEmpty(obj.APPLNO))
                    {
                        concast.AppendFormat(" AND APPLNO LIKE '%{0}%' ", obj.APPLNO.Replace("'", "''"));
                       // hashtable.Add(":APPLNO", obj.APPLNO);
                    }

                    if (!String.IsNullOrEmpty(obj.APPLNAME))
                    {
                        concast.AppendFormat(" AND APPLNAME LIKE '%{0}%' ", obj.APPLNAME.Replace("'","''"));
                        //hashtable.Add(":APPLNAME", obj.APPLNAME);
                    }

                    if (!String.IsNullOrEmpty(obj.DESCRIPTION))
                    {
                        concast.Append(" AND DESCRIPTION=:DESCRIPTION ");
                        hashtable.Add(":DESCRIPTION", obj.DESCRIPTION);
                    }

                    if (!Information.IsDate(obj.CRDATE))
                    {
                        concast.Append(" AND CRDATE=:CRDATE ");
                        hashtable.Add(":CRDATE", obj.CRDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.CRBY))
                    {
                        concast.Append(" AND CRBY=:CRBY ");
                        hashtable.Add(":CRBY", obj.CRBY);
                    }

                    if (!Information.IsDate(obj.UPDDATE))
                    {
                        concast.Append(" AND UPDDATE=:UPDDATE ");
                        hashtable.Add(":UPDDATE", obj.UPDDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.UPDBY))
                    {
                        concast.Append(" AND UPDBY=:UPDBY ");
                        hashtable.Add(":UPDBY", obj.UPDBY);
                    }

                    if (!String.IsNullOrEmpty(obj.URL))
                    {
                        concast.Append(" AND URL=:URL ");
                        hashtable.Add(":URL", obj.URL);
                    }

                    if (!Information.IsNothing(obj.PDAFLAG))
                    {
                        concast.Append(" AND PDAFLAG=:PDAFLAG ");
                        hashtable.Add(":PDAFLAG", obj.PDAFLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.MOBILEURL))
                    {
                        concast.Append(" AND MOBILEURL=:MOBILEURL ");
                        hashtable.Add(":MOBILEURL", obj.MOBILEURL);
                    }

                    if (!String.IsNullOrEmpty(obj.STATUS))
                    {
                        concast.Append(" AND STATUS=:STATUS ");
                        hashtable.Add(":STATUS", obj.STATUS);
                    }

                }
                sql = System.String.Format(@" SELECT COMPANY_CODE ,PRIVATE_FLAG ,MC_FLAG ,BUYER_FLAG ,NT_FLAG ,REMARK ,APPLNO ,APPLNAME ,DESCRIPTION ,CRDATE ,CRBY ,UPDDATE ,UPDBY ,URL ,PDAFLAG ,MOBILEURL ,STATUS  FROM APPLICATION");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<DataAccess.p_APPLICATION>(sql, hashtable, Conn).AsParallel();

            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                    page: page,
                    message: Core.Exception.ModalException(
                                        method: System.Reflection.MethodBase.GetCurrentMethod(),
                                        ex: ex));
                entities = null;
            }
            return entities;
        }

        #endregion
    }
}